// pages/changePhone/changePhoneNumber/changePhoneNumber.js
const util = require('../../../utils/util');
const API = require('../../../common/constant');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: '',
    phone: '',
    sendStatus: false,
    seccond: 59
  },
  /**
   * 修改新的号码
   */
  getInput: function (e) {
    let that = this;
    let code = e.detail.value.checkcode;
    if (this.data.code == '') {
      wx.showToast({
        title: '请获取验证码',
        icon: "none",
        duration: 1500
      });
    } else if (code == '') {
      wx.showToast({
        title: '请输入验证码',
        icon: "none",
        duration: 1500
      });
    } else if (code.length != 6) {
      wx.showToast({
        title: '请输入正确的验证码格式',
        icon: "none",
        duration: 1500
      });
    } else {
      // 获取验证码
      if (code != this.data.code) {
        wx.showToast({
          title: '验证码错误',
          icon: "none",
          duration: 1500
        });
      } else {
        util.http({
          url: API.URL.USER.UPDATE_MOBILE,
          data: {
            mobile: that.data.phone,
            code: that.data.code
          },
          success: res => {
            // console.log('kkkaaaa');
            wx.showToast({
              title: '修改号码成功',
              icon: "none",
              duration: 1500
            });
            setTimeout(function () {
              wx.switchTab({
                url: '../../my/my'
              })
            }, 1500);
          },
          fail: rej => {
            console.log(rej);
            if (rej.msg.length != 0) {
              wx.showToast({
                title: rej.msg,
                icon: "none",
                duration: 1500
              });
            }
          }
        });
      }
    }
  },
  /**
   * 获取手机验证码
   */
  sendPhoneCode: function (e) {
    // console.log(e);
    let that = this;
    if (this.data.phone == '') {
      wx.showToast({
        title: '请输入手机号码',
        icon: "none",
        duration: 1500
      });
    } else if (this.data.phone.length != 11) {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: "none",
        duration: 1500
      });
    } else {
      util.http({
        url: API.URL.USER.FIND_MOBILECODE,
        data: {
          mobile: this.data.phone
        },
        success: res => {
          // console.log(res.data);
          wx.showToast({
            title: '发送验证码成功',
            icon: "none",
            duration: 1500
          });
          that.setData({
            code: res.data.bussData,
            sendStatus: true
          });
          app.showUserInfo('验证码已经成功发送', 'success', 1500);
          let time = setInterval(() => {
            that.setData({
              seccond: that.data.seccond - 1
            });
            if (that.data.seccond == 0) {
              clearInterval(time);
              that.setData({
                sendStatus: false,
                seccond: 59
              })
            }
          }, 1000);
          console.log('发送成功');
        },
        fail: rej => {
          console.log(rej);
        }
      });
    }
  },
  /**
   * 获取手机号
   */
  getPhone: function (e) {
    // console.log(e.detail.value);
    this.setData({
      phone: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})